// GroupAI.cpp: implementation of the CGroupAI class.
//
//////////////////////////////////////////////////////////////////////

#include "TestAI.h"
#include <algorithm>
//#include "MetalMap.h"
//#include "Sim/Units/UnitDef.h"
//#include "TestAI.h"
vector<int> TTAICore::SpotState;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
std::ostream & operator<<(std::ostream & wy, float3 & wsp) //w testai.h
{
	wy<<" x="<<wsp.x<<"\ty="<<wsp.y<<"\tz"<<wsp.z;
	return wy;
}
namespace std
{
    void _xlen()
    {}
    ; 
}


TTAICore::TTAICore() //niezwykle skomplikowany i rozbudowy konstruktor ;)
{
	gl=0;
}

TTAICore::~TTAICore() //jw tylko destruktor
{
	#if PISZE
	LOGUJGl ("Robie harakiri");
	delete gl;
	#endif
}
void TTAICore::InicZm()
{
	width=cb->GetMapWidth()*8;
	height=cb->GetMapHeight()*8;
	kf=new konfig(this,cb);
	kf->init();
}
void TTAICore::InitAI(IGlobalAICallback* callback, int team)
{
	srand ( time(NULL) );
    this->callback=callback;
    cb = callback->GetAICallback ();
    char c[200];
	sprintf(c,"TSI konfigurator by Tow");
    callback->GetAICallback()->SendTextMsg(c,0);
	gl=new Globalne;
	#if PISZE
	start=time(NULL);
    time_t now1;
    time(&now1);
    struct tm *now2;
    now2 = localtime(&now1);
    sprintf(c, "AI\\TSI\\Log\\%2.2d-%2.2d-%4.4d %2.2d%2.2d (%d).log",
            now2->tm_mon + 1, now2->tm_mday, now2->tm_year + 1900, now2->tm_hour,
            now2->tm_min, team);
    gl->loguj = new std::ofstream(c);
    cb->SendTextMsg(c, 1);
    *gl->loguj << "\tNazwa AI/wersja: " << AI_NAME << endl;
    LOGUJGl("\tData/Czas: " << asctime(now2) << endl);
    LOGUJGl("\tMapa: " << cb->GetMapName() << endl);
	//LOGUJGl("\tRozpoznanych plamek: "<<mt->NumSpotsFound<<endl);
    LOGUJGl("\tMod: " << cb->GetModName() << endl);
    LOGUJGl("\tNazwa pliku: \'" << c << "\'" << endl);
    LOGUJGl(endl);
    LOGUJGl("\tRozmiary mapy: " << cb->GetMapWidth() << " x " << cb->GetMapHeight() << endl);
    LOGUJGl(endl);
    LOGUJGl(setw(8) << cb->GetCurrentFrame() << " TTAICore::InitAI(" << cb
        << ", " << team << ")" << endl);
	#endif
	InicZm();

}
void TTAICore::strzalkuj (float3 p, float3 q, float grubosc, float strzala,
		int czas, int grupa)
{
	cb->CreateLineFigure(p,q,grubosc,strzala,czas,grupa);
}
void TTAICore::UnitCreated(int unit)
{}
void TTAICore::EnemyDamaged(int damaged,int attacker,float damage,float3 dir)
{}
void TTAICore::UnitFinished(int unit)
{}



void TTAICore::UnitDestroyed(int unit,int attacker)
{}

void TTAICore::EnemyEnterLOS(int enemy)
{}

void TTAICore::EnemyLeaveLOS(int enemy)
{}

void TTAICore::EnemyEnterRadar(int enemy)
{}

void TTAICore::EnemyLeaveRadar(int enemy)
{}

void TTAICore::EnemyDestroyed(int enemy,int attacker)
{}

void TTAICore::UnitIdle(int unit)
{}

void TTAICore::GotChatMsg(const char* msg,int player)
{
	kf->rob(msg);
}

void TTAICore::UnitDamaged(int damaged,int attacker,float damage,float3 dir)
{}

void TTAICore::UnitMoveFailed(int unit)
{}

int TTAICore::HandleEvent(int msg,const void* data)
{
	LOGUJGl (CZAS<<cb->GetCurrentFrame() << "\tObslugiwacz zdarzen"<<endl);
    switch (msg) {
		case AI_EVENT_UNITGIVEN: {
			ChangeTeamEvent* cte=(ChangeTeamEvent *)data;		
		        
               
                UnitCreated(cte->unit);
				UnitFinished(cte->unit);
        
    
			break;
		}
		case AI_EVENT_UNITCAPTURED:{
			ChangeTeamEvent* cte=(ChangeTeamEvent *)data;
			
		    	UnitDestroyed(cte->unit,0);
			
			break;
		}
	}
	return 0;
}

void TTAICore::Update()
{}
